package com.foreverwin.mes.production.controller;

import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.production.model.WorkCenterShift;
import com.foreverwin.mes.production.model.WorkCenterShiftView;
import com.foreverwin.mes.production.service.WorkCenterShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author syngna
 * @since 2018-12-14
 */
@RestController
@RequestMapping("/workCenterShifts")
public class WorkCenterShiftController {

    @Autowired
    private WorkCenterShiftService workCenterShiftService;

    /**
     * 查询工作中心已经绑定的班别
     *
     * @return 有返回
     */
    @GetMapping("/getWorkCenterShifts")
    public AjaxResult getWorkCenterShift(String workCenter) {

        List<WorkCenterShiftView> list = null;
        String site = CommonMethods.getSite();

        try {
            list = workCenterShiftService.getWCShiftList(site, workCenter);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 保存公用
     *
     * @param workCenterShift 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult workCenterShiftSave(@RequestBody WorkCenterShift workCenterShift) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();

        try {

            if (StringUtils.isBlank(workCenterShift.getWorkCenter())) {
                throw BusinessException.build("工作中心不能为空！");
            } else if (StringUtils.isBlank(workCenterShift.getShift())) {
                throw BusinessException.build("班次不能为空！");
            }

            workCenterShiftService.saveWorkCenterShift(site, user, workCenterShift);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("班次设定成功");
    }

    /**
     * 批量更新-失效
     *
     * @param handles 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchUpdateExpire")
    public AjaxResult batchUpdateExpire(@RequestBody List<String> handles) {

        String user = CommonMethods.getUser();
        try {

            workCenterShiftService.updateBatchHandles("N", user, new Date(), handles);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success("操作成功");
    }

    /**
     * 批量更新-有效
     *
     * @param handles 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchUpdateEffect")
    public AjaxResult batchUpdateEffect(@RequestBody List<String> handles) {

        String user = CommonMethods.getUser();
        try {

            workCenterShiftService.updateBatchHandles("Y", user, new Date(), handles);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("操作成功");
    }

}